Repository navigation
fix: handle null-prototype objects and NaN in shallow equality - #295
Conversation
mbret
left a comment
There was a problem hiding this comment.
This fixes #268 as described. The inline comments ask for small changes only.
npm run check,npm run buildandnpm run test:cipass on the head commit (327 tests).- The 6 new tests fail on
mainand pass on this branch. Object.hasOwnis already used increateLocalStorageAdapter.ts, so the runtime requirements stay the same.
Generated by Claude Code
| @@ -1,18 +1,21 @@ | |||
| export function isShallowEqual<A, B>(objA: A, objB: B): boolean { | |||
| // Check if both objects are null or undefined | |||
There was a problem hiding this comment.
Could this comment say "either"? The condition now returns early when either value is null or undefined.
| // Check if both objects are null or undefined | |
| // Return early if either value is null or undefined |
Generated by Claude Code
| if ( | ||
| objA === null || | ||
| objB === null || | ||
| objA === undefined || | ||
| objB === undefined | ||
| ) { |
There was a problem hiding this comment.
The objB === null check also fixes a second crash, and no test covers it. On main, isShallowEqual(Object.create(null), null) throws TypeError: Cannot convert undefined or null to object. Could you add a regression test to shallowEqual.test.ts?
it("compares a null-prototype object with null", () => {
expect(isShallowEqual(Object.create(null), null)).toBe(false)
})Generated by Claude Code
| it("compares nested objects by reference without inspecting Date or Map contents", () => { | ||
| const value = { x: 1 } | ||
| expect(isShallowEqual({ value }, { value })).toBe(true) | ||
| expect(isShallowEqual({ value }, { value: { x: 1 } })).toBe(false) | ||
| expect(isShallowEqual(new Date(0), new Date(1))).toBe(true) | ||
| expect(isShallowEqual(new Map([["x", 1]]), new Map([["x", 2]]))).toBe(true) | ||
| }) |
There was a problem hiding this comment.
Could you remove the Date and Map assertions? They make a known limitation part of the tested contract. The docs already state the limitation. If a later change compares Date contents, this test fails although the change is correct.
| it("compares nested objects by reference without inspecting Date or Map contents", () => { | |
| const value = { x: 1 } | |
| expect(isShallowEqual({ value }, { value })).toBe(true) | |
| expect(isShallowEqual({ value }, { value: { x: 1 } })).toBe(false) | |
| expect(isShallowEqual(new Date(0), new Date(1))).toBe(true) | |
| expect(isShallowEqual(new Map([["x", 1]]), new Map([["x", 2]]))).toBe(true) | |
| }) | |
| it("compares nested objects by reference", () => { | |
| const value = { x: 1 } | |
| expect(isShallowEqual({ value }, { value })).toBe(true) | |
| expect(isShallowEqual({ value }, { value: { x: 1 } })).toBe(false) | |
| }) |
Generated by Claude Code
|
|
||
| ### Comparison semantics | ||
|
|
||
| `NaN` now compares equal to `NaN`, while positive and negative zero compare differently. Previously this helper used `===`, which did the opposite for these cases. If your application treats both zero signs as the same value, normalize them to `0` before comparison or pass a custom `compareFn` with your intended equality rule. |
There was a problem hiding this comment.
Could this paragraph describe only the current behavior? "now" and "Previously" read as a changelog, and they become stale after the next release. The release notes come from the commit message.
| `NaN` now compares equal to `NaN`, while positive and negative zero compare differently. Previously this helper used `===`, which did the opposite for these cases. If your application treats both zero signs as the same value, normalize them to `0` before comparison or pass a custom `compareFn` with your intended equality rule. | |
| `NaN` compares equal to `NaN`, and positive and negative zero compare as different values. If your application treats both zero signs as the same value, normalize them to `0` before comparison, or pass a custom `compareFn`. |
Generated by Claude Code
|
Addressed the five inline comments in 43e1771:
All three required checks pass locally on Node 26.13.1, with 43 files / 328 tests. Validation used the exact canonical LF Git tree and unchanged locked dependencies. No comparator algorithm change was needed in this follow-up. Prepared with OpenAI Codex assistance. |
|
Thanks for the work <3 |
isShallowEqualthrows for null-prototype dictionaries and objects that shadowhasOwnProperty, turning auseObserveemission into an error. UseObject.hasOwnfor own-key checks andObject.isfor value comparisons, with regressions through both the utility anduseObserve. Comparison withnullis also covered in both argument orders.NaNequalsNaN, while+0and-0differ, including object properties. Callers that want both zero signs treated alike should normalize them to0or supply their owncompareFn. The docs clarify that this remains an own-enumerable-key comparison and does not inspectDate,MaporSetcontents. Those limitations are documented without assertions that would prevent a future improvement.Fixes #268.
Validation on Windows Node 26.13.1 with unchanged npm-ci locked dependencies:
TypeErroron original mainec7dfe8bb396daed50803e0935fe13ae0067bfec; reverse-order and ordinary-object/null controls behave as expected.npm run check: passed;npm run build: passed;npm run test:ci: 43 files / 328 tests passed.Prepared with OpenAI Codex assistance.